package com.meixiang.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.tool.Tool;

/**
 * 带搜索的标题栏
 *
 * @author LZRUI
 */
public class SearchTitleView extends FrameLayout implements View.OnClickListener {

    private ImageView ivReturn;
    private ImageView ivShoppingCar;
    private FrameLayout flSearch;
    private RelativeLayout rlView;
    private LinearLayout llKeyTag;
    private TextView tvHint;
    private TextView tvKey;

    private Context mContext;

    private SearchBarClickListener searchBarClickListener;

    public void setSearchBarClickListener(SearchBarClickListener searchBarClickListener) {
        this.searchBarClickListener = searchBarClickListener;
    }

    public SearchTitleView(Context context) {
        this(context, null);
    }

    public SearchTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("NewApi")
    public SearchTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
        initSize();
        initEvent();
    }

    private void initView() {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.layout_search_title_view, this, true);
        ivReturn = (ImageView) rootView.findViewById(R.id.iv_return);
        ivShoppingCar = (ImageView) rootView.findViewById(R.id.iv_shopping_car);
        flSearch = (FrameLayout) rootView.findViewById(R.id.fl_search);
        rlView = (RelativeLayout) rootView.findViewById(R.id.rl_view);
        tvHint = (TextView) rootView.findViewById(R.id.tv_hint);
        llKeyTag = (LinearLayout) rootView.findViewById(R.id.ll_key);
        tvKey = (TextView) rootView.findViewById(R.id.tv_key);
    }

    /**
     * 设置关键字
     * @param keyWord
     */
    public void setKeyWord(String keyWord){
        if (TextUtils.isEmpty(keyWord)){
            tvHint.setVisibility(View.VISIBLE);
            llKeyTag.setVisibility(View.GONE);
        }else{
            tvKey.setText(keyWord);
            tvHint.setVisibility(View.GONE);
            llKeyTag.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 将高度设置为整个屏幕高度的0.08
     */
    private void initSize() {
//        int height = DensityUtil.dip2px(this.getContext(),44);
//        FrameLayout.LayoutParams titleViewParams = (FrameLayout.LayoutParams) rlView.getLayoutParams();
//        titleViewParams.height = height;
//        rlView.setLayoutParams(titleViewParams);
    }

    private void initEvent() {
        ivReturn.setOnClickListener(this);
        ivShoppingCar.setOnClickListener(this);
        flSearch.setOnClickListener(this);
        llKeyTag.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (searchBarClickListener == null) {
            throw new RuntimeException("SearchTitleView 必须实现 SearchBarClickListener 接口,才能实现点击事件");
        }
        if (Tool.isFastDoubleClick()) return;
        switch (v.getId()) {
            case R.id.iv_return:
                searchBarClickListener.onReturnClick();
                break;
            case R.id.iv_shopping_car:
                searchBarClickListener.onShoppingCarClick();
                break;
            case R.id.fl_search:
                searchBarClickListener.onSearchClick();
                break;
            case R.id.ll_key:
                searchBarClickListener.onDeleteKeyTag();
                tvHint.setVisibility(View.VISIBLE);
                llKeyTag.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 清除标签
     */
    public void clearTag(){
        tvHint.setVisibility(View.VISIBLE);
        llKeyTag.setVisibility(View.GONE);
    }

    public interface SearchBarClickListener {
        void onReturnClick();

        void onSearchClick();

        void onShoppingCarClick();

        void onDeleteKeyTag();
    }

}
